package com.qiqi.ctbluetooth

import android.graphics.Color
import android.os.Build
import android.os.Build.*
import android.os.Environment
import com.blankj.utilcode.util.StringUtils
import com.dylanc.mmkv.MMKVOwner
import com.dylanc.mmkv.mmkvBool
import com.dylanc.mmkv.mmkvBytes
import com.dylanc.mmkv.mmkvFloat
import com.dylanc.mmkv.mmkvInt
import com.dylanc.mmkv.mmkvString
import com.qiqi.ctbluetooth.data.ChannelData
import com.qiqi.ctbluetooth.data.DacPointData
import com.qiqi.ctbluetooth.data.LcData
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

object UserDataLocal : MMKVOwner(mmapID = "settings") {
    var savePath by mmkvString(default = "")
    var endTime by mmkvString(default = "2023-12-31 23:59:59")

    var colorBg by mmkvInt(default = Color.BLACK)
    var waveformBg by mmkvInt(default = Color.GREEN)
    var baoluoBg by mmkvInt(default = Color.BLUE)
    var rulerBg by mmkvInt(default = Color.RED)
    var dacBg by mmkvInt(default = Color.YELLOW)
    var rulePoint by mmkvInt(default = Color.MAGENTA)
    var stepping by mmkvFloat(default = 0.5f)
    var globalDB by mmkvFloat(default = 0f)
    var globalBuff by mmkvInt(default = 0)
    var globalInhibition by mmkvInt(default = 0)

    var saveDacPoint by mmkvBool(default = false)
    var isOpenDAC by mmkvBool(default = false)

    var connectDevice by mmkvString(default = "")



    fun addConnectDevice(device:String){
        if (!StringUtils.isEmpty(device)){
            var connectDeviceList: MutableList<String> = connectDevice.split("#$%&").toMutableList()
            if (!connectDeviceList.contains(device)){
                connectDeviceList.add(device)
                connectDevice = connectDeviceList.joinToString("#$%&")
            }
        }

    }

    fun getConnectDeviceList(): MutableList<String> {
        return connectDevice.split("#$%&").toMutableList()
    }

    //所有通道列表 json
    var channelListJson by mmkvString(default = "[]")
    //当前选中的通道 默认为K2.5轨底通道
    var currentChannel by mmkvInt(default = 3)

    fun setChannelList(channelList: MutableList<ChannelData>){
        channelListJson = Json.encodeToString(channelList)

    }

    //所有通道列表
    fun getChannelList(): MutableList<ChannelData> {
        //序列化json
        var jsonUtil: Json = Json {
            ignoreUnknownKeys = true  //忽略未知字段
            coerceInputValues = true  //返回的json字段为空或赋值默认值
        }
        val data = jsonUtil.decodeFromString<MutableList<ChannelData>>(channelListJson)
        if (data.isEmpty()){
            return mutableListOf(
                ChannelData(
                    "轨腰K1.0", 1.0, 0.707, 0.707, 12.0,  4,
                    lcList  = mutableListOf(
                        LcData("100mm", 100,48,46),
                        LcData("150mm", 150,70,46),
                        LcData("200mm", 200,96,32),
                        LcData("250mm", 250,121,26),
                        LcData("300mm", 300,145,23)),
                    dacDefaultPointList = mutableListOf(
                        DacPointData(0f,173f,300,240),
                        DacPointData(45f,173f,300,240),
                        DacPointData(91f,119f,300,240),
                        DacPointData(137f,84f,300,240),
                        DacPointData(228f,40f,300,240),
                        DacPointData(314f,24f,300,240),
                    ),
                    dacPointList = mutableListOf(
                        DacPointData(0f,173f,300,240),
                        DacPointData(45f,173f,300,240),
                        DacPointData(91f,119f,300,240),
                        DacPointData(137f,84f,300,240),
                        DacPointData(228f,40f,300,240),
                        DacPointData(314f,24f,300,240),
                    )
                ),


                ChannelData("轨腰K0.8", 0.8,0.623,0.781,12.0,4,
                    lcList  = mutableListOf(
                        LcData("100mm", 100,48,46),
                        LcData("150mm", 150,70,46),
                        LcData("200mm", 200,96,32),
                        LcData("250mm", 250,121,26),
                        LcData("300mm", 300,145,23)),
                    dacDefaultPointList = mutableListOf(),
                    dacPointList = mutableListOf()
                    ),

                ChannelData("轨头K2.5", 2.5,0.928,0.371,12.0,3,
                    lcList  = mutableListOf(
                        LcData("100mm", 100,48,46),
                        LcData("150mm", 150,70,46),
                        LcData("200mm", 200,96,32),
                        LcData("250mm", 250,121,26),
                        LcData("300mm", 300,145,23)),
                    dacDefaultPointList = mutableListOf(
                        DacPointData(0f,219f,250,295),
                        DacPointData(54f,219f,250,295),
                        DacPointData(106f,95f,250,295),
                        DacPointData(161f,47f,250,295),
                        DacPointData(211f,36f,250,295),
                        DacPointData(308f,13f,250,295)
                    ),
                    dacPointList = mutableListOf(
                        DacPointData(0f,219f,250,295),
                        DacPointData(54f,219f,250,295),
                        DacPointData(106f,95f,250,295),
                        DacPointData(161f,47f,250,295),
                        DacPointData(211f,36f,250,295),
                        DacPointData(308f,13f,250,295)
                    )
                ),

                ChannelData("轨底K2.5", 2.5,0.928,0.371,12.0, 1,
                    lcList  = mutableListOf(
                        LcData("100mm", 100,48,46),
                        LcData("150mm", 150,70,46),
                        LcData("200mm", 200,96,32),
                        LcData("250mm", 250,121,26),
                        LcData("300mm", 300,145,23)),
                    dacDefaultPointList = mutableListOf(
                        DacPointData(0f,241f,150,390),
                        DacPointData(121f,241f,150,390),
                        DacPointData(162f,130f,150,390),
                        DacPointData(256f,105f,150,390),
                        DacPointData(315f,88f,150,390),
                    ),
                    dacPointList = mutableListOf(
                        DacPointData(0f,241f,150,390),
                        DacPointData(121f,241f,150,390),
                        DacPointData(162f,130f,150,390),
                        DacPointData(256f,105f,150,390),
                        DacPointData(315f,88f,150,390),
                    )
                ),

                ChannelData("K0通道", 0.0,0.0,1.0,0.0,3,
                    lcList  = mutableListOf(
                        LcData("100mm", 100,24,9),
//                        LcData("150mm", 150,37,9),
                        LcData("200mm", 200,50,9),
                        LcData("250mm", 250,63,9),
                        LcData("300mm", 300,76,9),
                        LcData("1000mm", 1000,270,1)
                    ),
                    dacDefaultPointList = mutableListOf(
                        DacPointData(0f,202f,250,160),
                        DacPointData(78f,202f,250,160),
                        DacPointData(158f,175f,250,160),
                        DacPointData(238f,154f,250,160),
                        DacPointData(319f,118f,250,160),
                    ),
                    dacPointList = mutableListOf(
                        DacPointData(0f,202f,250,160),
                        DacPointData(78f,202f,250,160),
                        DacPointData(158f,175f,250,160),
                        DacPointData(238f,154f,250,160),
                        DacPointData(319f,118f,250,160),
                    )
                ),

                ChannelData("爬坡K4.0", 4.0,0.97,0.241,12.0,0,
                    lcList  = mutableListOf(
                        LcData("50mm", 50,23,46),
                        LcData("100mm", 100,48,46),
                        LcData("150mm", 150,70,46),
                        LcData("200mm", 200,96,32),
                        LcData("250mm", 250,121,26),
                        LcData("300mm", 300,145,23)),
                    dacDefaultPointList = mutableListOf(),
                    dacPointList = mutableListOf()
            ))
        }
        return data
    }
    var sendData by mmkvBytes(default = byteArrayOf(
        "cc".toInt(16).toByte(),
        "aa".toInt(16).toByte(),
        "90".toInt(16).toByte(), //增益低位
        "01".toInt(16).toByte(), //增益高位
        "79".toInt(16).toByte(), //采样控制低位
        "00".toInt(16).toByte(),  //采样控制高位
        "1a".toInt(16).toByte(), //延时
        "00".toInt(16).toByte(), //单双控制 0单1双
        "00".toInt(16).toByte(),
        "00".toInt(16).toByte(),
        "00".toInt(16).toByte(),
        "00".toInt(16).toByte(), // 全局增益
        "00".toInt(16).toByte(),
        "00".toInt(16).toByte(),
        "ff".toInt(16).toByte(),
        "ff".toInt(16).toByte()))

    fun saveSendData(index:Int, value:Byte){
        var newData = sendData
        newData[index] = value
        sendData = newData
    }


    fun isDoubleChannel() : Boolean{
        //1双 0 单
        return sendData[7] == "00".toInt(16).toByte()
    }

    // 获取 Download 文件夹路径的方法
    fun getDownloadDirectoryPath(): String {
        return if (VERSION.SDK_INT >= VERSION_CODES.Q) {
            // Android 10 及更高版本
            val downloadDir = BaseApp.appContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
            downloadDir?.absolutePath ?: ""
        } else {
            // Android 9 及更低版本
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
        }
    }
}